import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:shop/shop/merchant/AddGoodType.dart';
import 'package:shop/shop/merchant/GoodsInfoEdit.dart';
import 'package:shop/shop/merchant/GoodsList.dart';
import 'package:shop/config/config.dart';
import 'package:shop/http/HttpUtil.dart';
import 'package:shop/route/my_route.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';

class GoodsTypePage extends StatefulWidget {
  GoodsTypePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _GoodsTypePage createState() => _GoodsTypePage();
}

class _GoodsTypePage extends State<GoodsTypePage> with SingleTickerProviderStateMixin {
  String merchantID="";         //商户ID
  SharedPreferences prefs;
  var TypeList = [];//商品类型集合
  @override
  void initState()  {
    super.initState();
    getTypeList()  ;
  }
  // 加载商户的商品分类
  getTypeList() async{
    prefs = await SharedPreferences.getInstance();
    merchantID = prefs.getString("merchantID");
    var res = await HttpUtil().get("/waimai/mGoodsType/getMerchantAllGoodsType?mId="+merchantID );
    TypeList = res['data'];
    // print(TypeList);
    setState(() {});
  }
  // 显示商品分类
  _showTypeList(){
    var list =new List<Widget>();
    for(var i=0;i<TypeList.length;i++){
      list.add(ListTile(
        leading: IconButton(icon: Icon(Icons.remove_circle_outline,color: Colors.red,), onPressed: () async {
          print("删除"+TypeList[i]['name']);
          showDialog<Null>(
            context: context,
            barrierDismissible: false,
            builder: (BuildContext context) {
              return new AlertDialog(
                title: new Text('删除商品类型'),
                content: Text('确认删除?'),
                actions: <Widget>[
                  new FlatButton(
                    child: new Text('取消'),
                    onPressed: (){
                      Navigator.of(context).pop();
                    },
                  ),
                  new FlatButton(
                    child: new Text('确定'),
                    onPressed: () async{
                      var x = await HttpUtil().get("/waimai/mGoodsType/deleteMGoodsType?id="+TypeList[i]['id']);
                      if(x['code']==200){
                        TypeList.remove(TypeList[i]);
                        Fluttertoast.showToast(msg: "删除成功");
                      }else{
                        Fluttertoast.showToast(msg: x['msg']);
                      }
                      Navigator.of(context).pop();
                      setState(() {});
                    },
                  ),
                ],
              );
            },
          ).then((val) {
            // print(val);
          });
        }),
        title: Text(TypeList[i]['name']),
        tileColor: Colors.white,
        trailing: IconButton(icon: Icon(Icons.border_color), onPressed: (){
          // print(TypeList[i]);
          MyRoute.replace(AddGoodType(typeName: TypeList[i]['name'],isEdit: true,mTypeId: TypeList[i]['mtype'],typeId: TypeList[i]['id'],));
        }),
      ));
    }
    return list;
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context , width: 464 , height: 820) ;
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text("商品分类")
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            GestureDetector(
              child: ListTile(
                leading: Icon(Icons.add,color: Colors.white,),
                title: Text("新增分类"),
                tileColor: Colors.red,
              ),
              onTap: (){
                // print("新增分类");
                MyRoute.replace(AddGoodType(isEdit: false,));
              },
            ),
            SizedBox(height: 10,),
            Column(
              children: _showTypeList(),
            )
            // ListTile(
            //   leading: IconButton(icon: Icon(Icons.remove_circle_outline,color: Colors.red,), onPressed: (){
            //     print("删除1号");
            //   }),
            //   title: Text("下午茶"),
            //   tileColor: Colors.white,
            //   trailing: IconButton(icon: Icon(Icons.border_color), onPressed: (){
            //     print("修改");
            //     MyRoute.push(AddGoodType(title: "下午茶",));
            //   }),
            // ), ListTile(
            //   leading: IconButton(icon: Icon(Icons.remove_circle_outline,color: Colors.red,), onPressed: (){
            //     print("删除2号");
            //   }),
            //   title: Text("招牌菜"),
            //   tileColor: Colors.white,
            //   trailing: IconButton(icon: Icon(Icons.border_color), onPressed: (){
            //     print("修改");
            //     MyRoute.push(AddGoodType(title: "招牌菜",));
            //   }),
            // ), ListTile(
            //   leading: IconButton(icon: Icon(Icons.remove_circle_outline,color: Colors.red,), onPressed: (){
            //     print("删除3号");
            //   }),
            //   title: Text("单人套餐"),
            //   tileColor: Colors.white,
            //   trailing: IconButton(icon: Icon(Icons.border_color), onPressed: (){
            //     print("修改");
            //     MyRoute.push(AddGoodType(title: "单人套餐",));
            //   }),
            // ),
          ],
        ),
      ),
    );
  }
}